package com.sniff.atp.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sniff.atp.modules.test.mapper.DemandReportMapper;
import com.sniff.atp.modules.test.mapper.PublishPlanMapper;
import com.sniff.atp.modules.test.model.DemandReport;
import com.sniff.atp.modules.test.model.PublishPlan;
import com.sniff.atp.modules.test.service.PublishPlanService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author macro
 * @since 2023-10-12
 */
@Service
public class PublishPlanServiceImpl extends ServiceImpl<PublishPlanMapper, PublishPlan> implements PublishPlanService {

    @Autowired
    private PublishPlanMapper publishPlanMapper;

    @Autowired
    private DemandReportMapper demandReportMapper;

    @Override
    public boolean create(PublishPlan publishPlan) {
        publishPlan.setCreateTime(new Date());
        publishPlan.setUpdateTime(new Date());
        return save(publishPlan);
    }

    @Override
    public boolean update(Long id, PublishPlan publishPlan) {
        publishPlan.setPublishPlanId(id);
        publishPlan.setUpdateTime(new Date());
        return updateById(publishPlan);
    }

    @Override
    public Page<PublishPlan> list(PublishPlan publishPlan, Integer pageSize, Integer pageNum) {
        Page<PublishPlan> page = new Page<>(pageNum,pageSize);
        QueryWrapper<PublishPlan> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StringUtil.isNotBlank(publishPlan.getDemandBugName()), PublishPlan::getDemandBugName,publishPlan.getDemandBugName())
                .eq(StringUtil.isNotBlank(publishPlan.getPublishFlag()),PublishPlan::getPublishFlag,publishPlan.getPublishFlag())
                .eq(StringUtil.isNotBlank(publishPlan.getServiceName()),PublishPlan::getServiceName,publishPlan.getServiceName())
                .ge(publishPlan.getStartTime() != null,PublishPlan::getCreateTime,publishPlan.getStartTime())
                .le(publishPlan.getEndTime() != null,PublishPlan::getCreateTime,publishPlan.getEndTime())
                .orderByDesc(PublishPlan::getCreateTime);
        return page(page, wrapper);

    }

    @Override
    public List<PublishPlan> qryBusinessPublishPlan(PublishPlan publishPlan) {

        QueryWrapper<PublishPlan> wrapper = new QueryWrapper<>();

        //通知业务
        wrapper.lambda()
                .eq(PublishPlan::getInfoTime,publishPlan.getInfoTime())
                .eq(PublishPlan::getBusinessInformFlag,"通知")
                .eq(PublishPlan::getPublishFlag,"已发布")
                .orderByDesc(PublishPlan::getPrefixDemandBug);
//                .eq(StringUtil.isNotBlank(publishPlan.getTestCheckStatus()),PublishPlan::getTestCheckStatus,publishPlan.getTestCheckStatus())
        List<PublishPlan> publishPlanList = publishPlanMapper.selectList(wrapper);
        return publishPlanList;

    }

    @Override
    public List<PublishPlan> qrydevelopPublishPlan(PublishPlan publishPlan) {
        QueryWrapper<PublishPlan> wrapper = new QueryWrapper<>();

        //通知技术
        wrapper.lambda()
                .eq(PublishPlan::getInfoTime,publishPlan.getInfoTime())
                .eq(PublishPlan::getResearchInformFlag,"通知")
                .eq(PublishPlan::getPublishFlag,"已发布")
                .orderByDesc(PublishPlan::getPrefixDemandBug);
//                .eq(StringUtil.isNotBlank(publishPlan.getTestCheckStatus()),PublishPlan::getTestCheckStatus,publishPlan.getTestCheckStatus())
        List<PublishPlan> publishPlanList = publishPlanMapper.selectList(wrapper);
        return publishPlanList;

    }

    @Override
    public List<DemandReport> qryTestPublishPlan(PublishPlan publishPlan) {

        QueryWrapper<DemandReport> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                    .eq(DemandReport::getPublishDate,publishPlan.getInfoTime())
                    .orderByDesc(DemandReport::getDemandType);
        List<DemandReport> demandReportList = demandReportMapper.selectList(wrapper);

        return demandReportList;

    }

    @Override
    public List<DemandReport> qryTodaySUbmitTestInfo(PublishPlan publishPlan) {

        QueryWrapper<DemandReport> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                    .eq(DemandReport::getSubmitApplicationDate,publishPlan.getInfoTime())
                    .in(DemandReport::getDemandStatus,"开发中","测试中")
                    .orderByDesc(DemandReport::getDemandType);
        List<DemandReport> demandReportList = demandReportMapper.selectList(wrapper);

        return demandReportList;

    }

}
